package design.simdi.code.final_paper.site.panels.blog;

import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.wizard.WizardModel;
import org.apache.wicket.extensions.wizard.WizardStep;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import design.simdi.code.final_paper.core.entities.Mesaj;
import design.simdi.code.final_paper.core.entities.Person;
import design.simdi.code.final_paper.core.entities.User;
import design.simdi.code.final_paper.core.enums.MessageStatus;
import design.simdi.code.final_paper.core.services.ApplicationService;
import design.simdi.code.final_paper.core.services.ServiceException;
import design.simdi.code.final_paper.core.services.UserService;
import design.simdi.code.final_paper.site.application.MyAuthenticatedWebSession;
import design.simdi.code.final_paper.site.application.WicketApplication;
import design.simdi.code.final_paper.site.panels.MyWizard;
import design.simdi.code.final_paper.site.panels.MyWizardButtonBar;

/**
 * 
 * @author Marius Dumitriu - (Jun 21, 2010 - 4:12:59 PM)
 * 
 */
public class TrimiteMesajWizardPanel extends MyWizard {

	private static final long serialVersionUID = 1L;

	private TextField<String> dest;
	private TextField<String> exped;
	private TextField<String> subject;
	private TextArea<String> content;
	private Component backComponent;
	private String title;
	private String subtitle;
	private Mesaj mesaj;
	@SpringBean
	private ApplicationService applicationService;

	@SpringBean
	private UserService userService;

	public TrimiteMesajWizardPanel(String id, Person person, Component back) {
		super(id, false);

		title = "form.add.title";
		subtitle = "form.add.subtitle";
		this.backComponent = back;
		mesaj = new Mesaj();

		User currentUser = ((MyAuthenticatedWebSession) getSession()).getCurrentUser();

		Person currentPerson = userService.findPersonByUser(currentUser);

		mesaj.setExpeditor(currentPerson);
		mesaj.setDestinatar(person);
		mesaj.setStatus(MessageStatus.NEW);

		setDefaultModel(new CompoundPropertyModel<TrimiteMesajWizardPanel>(this));
		WizardModel model = new WizardModel();
		model.add(new MesajStep());

		init(model);
		((WicketApplication) getApplication()).setShowErrors(false);
	}

	protected Component newButtonBar(String id) {
		return new MyWizardButtonBar(id, this, getForm()) {

			private static final long serialVersionUID = 1L;

			public void onFinishClick(AjaxRequestTarget target) {
				try {
					onFinish(target);

					Page page = this.getPage();

					page.get("content:tabs:panel").replaceWith(backComponent);

					target.addComponent(page.get("content"));

				} catch (ServiceException e) {

					error(e.getMessage());
				}

			}

			public void onCancelClick(AjaxRequestTarget target) {
				onCancel(target);

				Page page = this.getPage();

				page.get("content:tabs:panel").replaceWith(backComponent);

				target.addComponent(page.get("content"));
			}

		};
	}

	public void onFinish(AjaxRequestTarget target) throws ServiceException {

		applicationService.saveMesaj(mesaj);
		onFinishMessage(target);
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}

	public ApplicationService getApplicationService() {
		return applicationService;
	}

	private final class MesajStep extends WizardStep {

		private static final long serialVersionUID = 1L;

		public MesajStep() {
			super(new ResourceModel(title), new ResourceModel(subtitle));

			add(dest = new TextField<String>("dest", new PropertyModel<String>(mesaj.getDestinatar(), "fullName")));
			add(exped = new TextField<String>("exped", new PropertyModel<String>(mesaj.getExpeditor(), "fullName")));
			add(subject = new TextField<String>("subject", new PropertyModel<String>(mesaj, "title")));
			add(content = new TextArea<String>("content", new PropertyModel<String>(mesaj, "content")));

			subject.setRequired(true);
			content.setRequired(false);

			subject.setType(String.class);
			content.setType(String.class);

			dest.setEnabled(false);
			exped.setEnabled(false);

		}

	}

}
